//
//  TestLGSName.m
//  LogicSim
//
//  Created by Jeremy Pereira on 26/02/2013.
//
//

#import "TestLGSName.h"
#import "LGSName.h"

@implementation TestLGSName

-(void) testCreateNoConnection
{
    LGSName* aName = [LGSName nameWithString: @"foo"];
    STAssertTrue([[aName nodePart] isEqualToString: @"foo"],
                 @"Name not created correctly %@", aName);
}

-(void) testIsEqual
{
    LGSName* name1 = [LGSName nameWithString: @"foo.bar"];
    LGSName* name2 = [LGSName nameWithString: @"foo.bar"];
    LGSName* name3 = [LGSName nameWithString: @"bar.foo"];
    STAssertTrue([name1 isEqual: name2], @"Names not equal");
    STAssertFalse([name3 isEqual: name2], @"Names not equal");
}

-(void) testIsEqualNoConnection
{
    LGSName* name1 = [LGSName nameWithString: @"foo"];
    LGSName* name2 = [LGSName nameWithString: @"foo"];
    LGSName* name3 = [LGSName nameWithString: @"foo.bar"];
    LGSName* name4 = [LGSName nameWithString: @"foo2"];
    STAssertTrue([name1 isEqual: name2], @"Names not equal");
    STAssertFalse([name3 isEqual: name2], @"Names not equal");
    STAssertFalse([name1 isEqual: name4], @"Names not equal");
}

-(void) testStringValue
{
    NSString* value = @"foo.bar";
    LGSName* name = [LGSName nameWithString: value];
    STAssertTrue([[name stringValue] isEqual: value], @"name does not have right stringValue %@", name);
}

-(void) testStringValueNodeIndexed
{
    NSString* value = @"foo.bar";
    LGSName* name = [LGSName nameWithString: value];
    name = [name nameWithNodeIndex: 1];
    STAssertTrue([[name stringValue] isEqual: @"foo[1].bar"], @"name does not have right stringValue %@", name);
	name = [name nameWithoutNodeIndex];
    STAssertTrue([[name stringValue] isEqual: value], @"name does not have right stringValue %@", name);
}

-(void) testParseIndex
{
    LGSName* name1 = [LGSName nameWithString: @"foo"];
    LGSName* name2 = [LGSName nameWithString: @"foo[0]"];
    LGSName* name3 = [LGSName nameWithString: @"foo[2]"];
    LGSName* name4 = [LGSName nameWithString: @"foo[1].bar[2]"];
    STAssertTrue([name1 nodeIndex] == LGS_NAME_NO_INDEX, @"Incorrect index for name1");
    STAssertTrue([name2 nodeIndex] == 0, @"Incorrect index for name2");
    STAssertTrue([name3 nodeIndex] == 2, @"Incorrect index for name3");
    STAssertTrue([name4 nodeIndex] == 1, @"Incorrct node index for name4");
    STAssertTrue([name4 connectionIndex] == 2, @"Incorrct connection index for name4");
    STAssertTrue([[name4 nodePart] isEqual: @"foo"], @"Incorrct node part for name4");
    STAssertTrue([[name4 connectionPart] isEqual: @"bar"], @"Incorrct connection part for name4");
}


-(void) testCompare0
{
    LGSName* name1 = [LGSName nameWithString: @"foo"];
    LGSName* name2 = [LGSName nameWithString: @"foo[0]"];
    STAssertTrue([name1 isEqual: name2], @"Incorrect index for name1");
}

-(void) testParseConnectionIndex
{
    LGSName* name1 = [LGSName nameWithString: @"foo.bar"];
    LGSName* name2 = [LGSName nameWithString: @"foo.bar[0]"];
    LGSName* name3 = [LGSName nameWithString: @"foo.bar[2]"];
    STAssertTrue([name1 nodeIndex] == LGS_NAME_NO_INDEX, @"Incorrect index for name1");
    STAssertTrue([name1 connectionIndex] == LGS_NAME_NO_INDEX, @"Incorrect index for name2");
    STAssertTrue([name2 connectionIndex] == 0, @"Incorrect index for name2");
    STAssertTrue([name3 connectionIndex] == 2, @"Incorrect index for name3");
}


-(void) testCompareConnectionIndex0
{
    LGSName* name1 = [LGSName nameWithString: @"foo.bar"];
    LGSName* name2 = [LGSName nameWithString: @"foo.bar[0]"];
    STAssertTrue([name1 isEqual: name2], @"Incorrect index for name1");
}

-(void) testConnection
{
    LGSName* name = [LGSName nameWithConnectionString: @"bar"];
    STAssertTrue([[name connectionPart] isEqual: @"bar"], @"Connection part wrong");
    STAssertTrue([name nodePart] == nil, @"Node part wrong");
    LGSName* name1 = [[LGSName nameWithString: @"foo.bar"] nameWithoutNode];
    STAssertTrue([name isEqual: name1], @"Names not equal %@ and %@", name, name1);
}

-(void) testProperties
{
    LGSName* name1 = [LGSName nameWithString: @"a.b"];
    STAssertTrue([[name1 nameWithoutNode] isEqual: [LGSName nameWithConnectionString: @"b"]],
                         @"nameWithoutNode broken");
}

@end
